package com.data.util;

import com.data.dto.CV;
import com.data.dto.Education;
import com.data.dto.Experience;
import com.data.io.TemplateLeft;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;


public class PoiUtil {

    public static InputStream createWord(CV cv) {

        try (FileInputStream is = new FileInputStream(new File("template.docx"));
             final OPCPackage pkg = OPCPackage.open(is);
             ) {
            StringBuilder builder = new StringBuilder();
            final PackagePart part = pkg.getPart(PackagingURIHelper.createPartName("/word/document.xml"));

            builder.append(TemplateLeft.header)
                    .append(TemplateLeft.formatName(cv.getName()))
                    .append(TemplateLeft.formatEmail(cv.getEmail()))
                    .append(TemplateLeft.formatAddress(cv.getAddress()))
                    .append(TemplateLeft.formatTel(cv.getTel()))
                    .append(TemplateLeft.formatSummary(cv.getSummary()))
                    .append(TemplateLeft.expTitle);
            for (Experience exp : cv.getExperienceList()) {
                builder.append(TemplateLeft.formatExpEmployer(exp.getEmployer()))
                        .append(TemplateLeft.formatExpPeriod(exp.getPeriod()))
                        .append(TemplateLeft.formatExpTitle(exp.getTitle()))
                        .append(TemplateLeft.formatExpDuty(exp.getDuty()));
            }
            builder.append(TemplateLeft.eduTitle);
            for (Education edu : cv.getEducationList()) {
                builder.append(TemplateLeft.formatEduSchool(edu.getSchool(), edu.getDegree()))
                        .append(TemplateLeft.formatEduPeriod(edu.getPeriod()))
                        .append(TemplateLeft.formatEduMajor(edu.getMajor()))
                        .append(TemplateLeft.formatEduGPA(edu.getGpa() == null || edu.getGpa().equals("") ? "" : "GPA：" + edu.getGpa()))
                        .append(TemplateLeft.formatEduActivity(edu.getActivity() == null || edu.getActivity().equals("") ? "" : "社团活动：" + edu.getActivity()));
            }
            builder.append(TemplateLeft.skillTitle)
                    .append(TemplateLeft.formatSkill(cv.getSkill()))
                    .append(TemplateLeft.end);

            try (OutputStream partOS = part.getOutputStream()) {
                partOS.write(builder.toString().getBytes("UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            final URIConverter uriConverter = new ExtensibleURIConverterImpl();
            final MemoryURIHandler handler = new MemoryURIHandler();
            uriConverter.getURIHandlers().add(0, handler);
            XWPFDocument res = new XWPFDocument(pkg);
            final URI memoryURI = URI
                    .createURI(MemoryURIHandler.PROTOCOL + "://resources/temp.docx");
            POIServices.getInstance().saveFile(uriConverter, res, memoryURI);
            InputStream source = uriConverter.createInputStream(memoryURI) ;
                return source;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {

        }
    }
}
